package com.cs.beneficios.web.action;
import com.cs.beneficios.business.entity.UsuarioSubprogramaDAO;
import com.cs.beneficios.business.model.UsuarioSubprogramaVO;
import com.cs.beneficios.common.ConnectionFactory;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.LinkedList;
import java.util.List;
import javax.servlet.http.HttpSession;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionMapping;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.ActionForward;
//import org.apache.struts.action.ActionError;
//import org.apache.struts.action.ActionErrors;
import java.io.IOException;
import javax.servlet.ServletException;

public class SubproUsuarioAction extends Action 
{
  /**
   * This is the main action called from the Struts framework.
   * @param mapping The ActionMapping used to select this instance.
   * @param form The optional ActionForm bean for this request.
   * @param request The HTTP Request we are processing.
   * @param response The HTTP Response we are processing.
   * @return 
   * @throws java.io.IOException
   * @throws javax.servlet.ServletException
   */
  public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException
  {
    HttpSession session = request.getSession();
    //ConnectionFactory cf = new ConnectionFactory("beneficios","beneficios");
	ConnectionFactory cf = new ConnectionFactory();
    String codeSubpro = (String)session.getAttribute("codeSubprograma");
    String codeSede = (String)session.getAttribute("codeSede");
    String codeProg = (String)session.getAttribute("codePrograma");
    //Proceso para identificar si el subprograma ya esta asociado a una sede
    List lRegistrado = new LinkedList();
    List lError = new LinkedList();
    UsuarioSubprogramaDAO objSubproDAO = new UsuarioSubprogramaDAO(cf);
    
	if(codeSubpro == null && codeSede == null && codeProg ==null){
	  String codeSubSede = (String)session.getAttribute("codeSubSede");
      
	  if(codeSubSede != null){
		try {
			codeSubpro = objSubproDAO.getSubprograma(codeSubSede);
			codeSede = objSubproDAO.getSede(codeSubSede);
			codeProg = objSubproDAO.getPrograma(codeSubSede);
            
			session.setAttribute("codeSubprograma", codeSubpro);
			session.setAttribute("codeSede", codeSede);
			session.setAttribute("codePrograma", codeProg);
            
			session.removeAttribute("codeSubSede");
            
		} catch (SQLException e) {
			System.err.println("/******** TDSUB - cwsub");
			e.printStackTrace();
			System.err.println("TDSUB - cwsub ********/");
		}
	  }
	}
    
    int CodeSubSede = 0;
    try
    {
      CodeSubSede = objSubproDAO.GetSubprogSede(codeSubpro,codeSede);
      if(CodeSubSede != 0)
      {
        //Proceso para asignar una sede a un usuario
        List lUsuarios = new LinkedList();
        lUsuarios = (List)session.getAttribute("ListaUsuarios");
        for(int i=0;i<lUsuarios.size();i++)
        {
          String seleccion = "codeusuario"+i;
          Object ocoduser = request.getParameter(seleccion);
          if(ocoduser!=null)
          {
            String CodeUsuario = (String)request.getParameter(seleccion);
            boolean verificar = objSubproDAO.Validar(CodeUsuario,CodeSubSede);
            ResultSet rst = objSubproDAO.GetNombres(codeSubpro,codeSede,codeProg,CodeUsuario);
            UsuarioSubprogramaVO ususubproVO;
            if(!verificar)
            {
              int Insertobien = objSubproDAO.InsertUsuarioSubpro(CodeSubSede,CodeUsuario);
              
              if(rst.next())
              {
                ususubproVO = new UsuarioSubprogramaVO();
                ususubproVO.setNombreprog(rst.getString("nomprog"));
                ususubproVO.setNombresubpro(rst.getString("nomsub"));
                ususubproVO.setNombresede(rst.getString("nomsede"));
                ususubproVO.setNombreUsuario(rst.getString("nomusu"));
				ususubproVO.setNombreGrupo(rst.getString("descripGrupo"));
                lRegistrado.add(ususubproVO);
              }
            }else
            {
              if(rst.next())
              {
                ususubproVO = new UsuarioSubprogramaVO();
                ususubproVO.setNombreprog(rst.getString("nomprog"));
                ususubproVO.setNombresubpro(rst.getString("nomsub"));
                ususubproVO.setNombresede(rst.getString("nomsede"));
                ususubproVO.setNombreUsuario(rst.getString("nomusu"));
				ususubproVO.setNombreGrupo(rst.getString("descripGrupo"));
                lError.add(ususubproVO);
              }
            }
            rst.close();      
          }
        }
        if(lRegistrado.size()>0)
        {
          request.setAttribute("listaregistros",lRegistrado);
        }
        if(lError.size()>0)
        {
          request.setAttribute("listaerror",lError);
        }
        //Fin del proceso
      }else
      {
        return mapping.findForward("registrar");
      }
      cf.closeConnection(objSubproDAO.getConn());
    }catch(SQLException e)
    {
		System.err.println("/******** TDSUB - cwsub");
		e.printStackTrace();
		System.err.println("TDSUB - cwsub ********/");
    }
    //fin del proceso
    
    
    return mapping.findForward("success");
  }
}